/*  Copyright (C) 2014-2018 FastoGT. All right reserved.
    This file is part of iptv_cloud.
    iptv_cloud is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    iptv_cloud is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with iptv_cloud.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <common/license/utils.h>

#include <errno.h>
#include <unistd.h>
#include <uuid/uuid.h>

namespace common {
namespace license {

bool GetMachineID(std::string* serial) {
  if (!serial) {
    return false;
  }

  struct timespec ts;
  ts.tv_sec = 1;
  ts.tv_nsec = 0;
  uuid_t uuid = {0};

  if (gethostuuid(uuid, &ts) == -1) {
    switch (errno) {
      case EFAULT:
        return false;
      case EWOULDBLOCK:
        return false;
    }
  }

  uuid_string_t uuid_string;
  uuid_unparse_lower(uuid, uuid_string);
  *serial = uuid_string;
  return true;
}

}  // namespace license
}  // namespace common
